package com.wdd.recoveryvehicle.vehicleInfo.service.impl;

import com.wdd.recoveryvehicle.mapper.VehicleInfoMapper;
import com.wdd.recoveryvehicle.vehicleInfo.bean.VehicleInfoVB;
import com.wdd.recoveryvehicle.vehicleInfo.service.VehicleInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

@Service
public class VehicleInfoServiceImpl implements VehicleInfoService {

    private Logger logger = LoggerFactory.getLogger(VehicleInfoServiceImpl.class);

    @Autowired
    private VehicleInfoMapper vehicleInfoMapper;

    @Override
    public boolean insertVehicleInfo(VehicleInfoVB vehicleInfoVB, String user_id, String company_id) {
        return vehicleInfoMapper.insertVehicleInfo(vehicleInfoVB, user_id, company_id);
    }

    @Override
    public boolean insertVehicleEmpRelation(String emp_id, String vehicle_id) {
        return vehicleInfoMapper.insertVehicleEmpRelation(emp_id, vehicle_id);
    }

    @Override
    public List<VehicleInfoVB> selectVehicleInfoVBByUseful(String useful, String company_id) {
        return vehicleInfoMapper.selectVehicleInfoVBByUseful(useful, company_id);
    }

    @Override
    public List<VehicleInfoVB> selectVehicleInfoVBByEmpId(String emp_id) {
        List<String> vehicleIds = vehicleInfoMapper.selectVehicleIdsByEmpId(emp_id);
        List<VehicleInfoVB> vehicleInfoVBS = new ArrayList<VehicleInfoVB>();
        if (vehicleIds!=null && vehicleIds.size()>0){
            for (String vehicle_id : vehicleIds){
                VehicleInfoVB vehicleInfoVB = vehicleInfoMapper.selectVehicleInfoVBByVehicleId(vehicle_id);
                vehicleInfoVBS.add(vehicleInfoVB);
            }
        }
        return vehicleInfoVBS;
    }

    @Override
    public boolean updateVehicleInfo(VehicleInfoVB vehicleInfoVB, String user_id) {
        return vehicleInfoMapper.updateVehicleInfo(vehicleInfoVB, user_id);
    }

    @Override
    public boolean updateEmpVehicleRelation(String vehicle_id, String emp_id) {
        boolean flag1 = vehicleInfoMapper.updateEmpVehicleRelation(vehicle_id); // 更新成无效的
        boolean flag2 = vehicleInfoMapper.insertVehicleEmpRelation(emp_id, vehicle_id); // 新增 一个关系
        boolean flag3 = vehicleInfoMapper.distinctEmpVehicleRelation(); // 删除重复的人与车的对应关系
        return flag1 && flag2 & flag3;
    }

    @Override
    public boolean updateVehicleInfoUsefulWx(String vehicle_id, String user_id) {
        boolean flag1 = vehicleInfoMapper.updateVehicleInfoUsefulWx(vehicle_id, user_id);
        boolean flag2 = vehicleInfoMapper.updateEmpVehicleRelation(vehicle_id); // 更新成无效的
        boolean flag3 = vehicleInfoMapper.distinctEmpVehicleRelation(); // 删除重复的人与车的对应关系
        return flag1 && flag2 & flag3;
    }
}
